package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishSetMeal;
import com.sky.mapper.DishStu;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import javassist.bytecode.StackMapTable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import javax.swing.text.Utilities;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private DishStu dishStu;

    @Autowired
    private DishSetMeal dishSetMeal;
    @Override
    public void insert(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
//1.添加一个菜品
        dishMapper.insertDish(dish);
//2.添加到对应菜品的口味表上(添加n个菜品口味)
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if(flavors!=null && flavors.size()>0){
            Long dishId = dish.getId();
            /**
             因为DishFlavor对象的dishId为空的，所以要拿到菜品的主键id赋值
             *
              */
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertDishFlavor(flavors);
        }
    }

    @Override
    public PageResult SelectQuery(DishPageQueryDTO dishPageQueryDTO) {
        //利用PageHelper来分页查询
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page= dishMapper.SelectQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteDish(List<Long> ids) {
        //判断被删除的菜品是否起售
        List<Dish> dishlist=dishStu.selectStuByids(ids);
        for (Dish dish: dishlist ) {
            if (dish.getStatus()!= StatusConstant.DISABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断被删除的菜品是否关联套餐
        List<Long> dishList=dishSetMeal.selectSetMeal(ids);
        if(dishList!=null && dishList.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //批量删除菜品关联的口味
        dishFlavorMapper.deleteFlavorById(ids);

        //根据id批量删除菜品
        dishMapper.delteDish(ids);


    }

    @Override
    public DishVO getById(Long id) {
        //根据id查询菜品的基本信息
        Dish dish=dishMapper.getById(id);
        //根据id查询菜品的口味数据
        List<DishFlavor> dishFlavors=dishFlavorMapper.getByDishId(id);
        //最后封装到dishVo中
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public void UploadDish(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //1.先修改除了菜品口味外的基本菜品数据
        dishMapper.UploadDish(dish);
        //2.再修改菜品口味数据
        /**
         * 这里的菜品口味修改需要注意一点:
         * 1.先删除原有的口味数据
         * 2.再来修改口味数据
         */
        dishFlavorMapper.delDishFlavorById(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && flavors.size()>0){
         /**
          为什么要给每一个口味赋值dishId,因为你假如新增口味，
          根据需求文档，前端只上传了口味对象(DishFlavor）的name和value参数，并没有上传dishId参数
          不然在数据库中会出现口味关系表数据dishId字段为空,就不能根据dishId查询菜品对应的口味了
          所以我们手动获取菜品(Dish)的id赋值给口味对象(DishFlavor)的dishId,通过for循环遍历List对象集合
          */
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertDishFlavor(flavors);
        }


    }

    @Override
    public void setStatusDish(Integer status, Long id) {
        Dish dish = Dish.builder().status(status).id(id).build();
        dishMapper.UploadDish(dish);
    }
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }


}
